package address

import (
	"errors"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
)

// 获取用户的默认地址，若未设置则直接返回最早添加的地址
func Default(uid uint) (*model.Address, error) {
	mod := model.Address{}
	conn, err := config.GetConnect()
	if err != nil {
		return nil, err
	}
	err = conn.Table(mod.TableName()).Where("uid", uid).Select("is_deleted", 0).Order("is_default DESC,id ASC").Limit(1).Find(&mod).Error
	if err != nil {
		return nil, err
	}
	if mod.Id == 0 {
		return nil, errors.New("用户地址为空")
	}
	return &mod, nil
}
